package com.wxq.yaoxia.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

public class YXUtility {
	
	public static void copyFile(String from,String to) {
    	
        FileInputStream  fis = null;
        FileOutputStream fos = null;
        //File file = new File(android.os.Environment.getExternalStorageDirectory()+"/serverconf.xml");
		try 
		{
			fis = new FileInputStream(from);
			fos = new FileOutputStream(to);
			byte [] buffer = new byte[8192];
			
			int count = 0;
			while((count = fis.read(buffer))>=0)
			{
				fos.write(buffer, 0, count);
			}
			fos.close();
			fis.close();
		} catch (IOException e) {
			Log.d("wxq", "Can NOT copy the file:"+from+" to:"+to);
			e.printStackTrace();
		}
	}

	public static void copyFileFromAssets(Context context,String fileInAssets,File outFile) {
    	AssetManager am = context.getAssets();
        InputStream is;
        FileOutputStream fos = null;
        //File file = new File(android.os.Environment.getExternalStorageDirectory()+"/serverconf.xml");
		try 
		{
			is = am.open(fileInAssets);
			fos = new FileOutputStream(outFile);
			byte [] buffer = new byte[8192];
			
			int count = 0;
			while((count = is.read(buffer))>=0)
			{
				fos.write(buffer, 0, count);
			}
			fos.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("wxq", "Can NOT copy the file:"+outFile.getName());
			e.printStackTrace();
		}
	}
	public static void copyFileFromAssets(Context context,String fileInAssets,String outFileName) {
		File outFile = new File(outFileName);
		copyFileFromAssets(context, fileInAssets, outFile);
	}
	
	public static boolean CreateFile(String destFileName) {
	    File file = new File(destFileName);
	    if (file.exists()) {
	    	System.out.println("创建单个文件" + destFileName + "失败，目标文件已存在！");
	    	return false;
	    }
	    if (destFileName.endsWith(File.separator)) {
	    	System.out.println("创建单个文件" + destFileName + "失败，目标不能是目录！");
	    	return false;
	    }
	    if (!file.getParentFile().exists()) {
	    	System.out.println("目标文件所在路径不存在，准备创建。。。");
	     if (!file.getParentFile().mkdirs()) {
	    	 System.out.println("创建目录文件所在的目录失败！");
	    	 return false;
	     }
	    }

	    // 创建目标文件
	    try {
	     if (file.createNewFile()) {
	    	 System.out.println("创建单个文件" + destFileName + "成功！");
	    	 return true;
	     } 
	     else {
	    	 System.out.println("创建单个文件" + destFileName + "失败！");
	    	 return false;
	     }
	    } 
	    catch (IOException e) {
	    	e.printStackTrace();
	    	System.out.println("创建单个文件" + destFileName + "失败！");
	    	return false;
	    }
	}
	
	public static boolean createDir(String destDirName) {
	    File dir = new File(destDirName);
	    if(dir.exists()) {
	    	System.out.println("创建目录" + destDirName + "失败，目标目录已存在！");
	    	return false;
	    }
	    if(!destDirName.endsWith(File.separator))
	    	destDirName = destDirName + File.separator;
	    // 创建单个目录
	    if(dir.mkdirs()) {
	    	System.out.println("创建目录" + destDirName + "成功！");
	    	return true;
	    } else {
	    	System.out.println("创建目录" + destDirName + "失败！");
	    	return false;
	    }
	}
	
	/** 
     * 进行MD5加密 
     *  
     * @param info 
     *            要加密的信息 
     * @return String 加密后的字符串 
     */  
    public static String encryptToMD5(String info) {  
        byte[] digesta = null;  
        try {  
            // 得到一个md5的消息摘要  
            MessageDigest alga = MessageDigest.getInstance("MD5");  
            // 添加要进行计算摘要的信息  
            alga.update(info.getBytes());  
            // 得到该摘要  
            digesta = alga.digest();  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        }  
        // 将摘要转为字符串  
        String rs = byte2hex(digesta);  
        return rs;  
    }  
      
    public static String encryptToMD52(String info) {  
        byte[] digesta = null;  
        try {  
            MessageDigest alga = MessageDigest.getInstance("MD5");  
            alga.update(info.getBytes());  
            digesta = alga.digest();  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        }  
        String rs = digesta.toString();  
        return rs;  
    }  
    /** 
     * 将二进制转化为16进制字符串 
     *  
     * @param b 
     *            二进制字节数组 
     * @return String 
     */  
    public static String byte2hex(byte[] b) {  
        String hs = "";  
        String stmp = "";  
        for (int n = 0; n < b.length; n++) {  
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  
            if (stmp.length() == 1) {  
                hs = hs + "0" + stmp;  
            } else {  
                hs = hs + stmp;  
            }  
        }  
        return hs.toUpperCase();  
    }  
    
    public static void showToast(Context context,String msg,Handler handler) {
    	handler.post(new ToastRunnable(context, msg));
	}
    static class ToastRunnable implements Runnable
    {
    	Context context;
    	String msg;	

		public ToastRunnable(Context context, String msg) {
			super();
			this.context = context;
			this.msg = msg;
		}

		@Override
		public void run() {
			Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
		}
    	
    }
    
}
